'use strict';
const http = require('../utils/httpUtils');
const namespace = require('./namespace');
module.exports = function (app) {

  //普通藏品详情
  app.get(`${namespace.root}/collectDetail`, async function (req, res) {
    let result = await http.get(`/api/mallProduct/web/query`, {
      id: req.query.id
    }, '/antique').catch(e => {
      res.send({
        errCode: 401
      });
    });
    res.render('collectDetail', {
      id: req.query.id,
      productDetail: result.data
    });
  });

  //拍卖藏品详情
  app.get(`${namespace.root}/auctionCollectDetail`, async function (req, res) {
    let result = await http.get(`/api/mallProduct/web/query`, {
      id: req.query.id
    }, '/antique').catch(e => {
      res.send({
        errCode: 401
      });
    });
    res.render('auctionCollectDetail', {
      id: req.query.id,
      productDetail: result.data
    });
  });

  //仓库藏品
  app.get(`${namespace.root}/collectList`, async function (req, res) {
    res.render('collectList', {
      proType: req.query.state
    });
  });

  //上传微拍藏品
  app.get(`${namespace.root}/collect/addAuction`, async function (req, res) {
    let proDetail = '';
    let auctionDetail = null;
    if (req.query.auctionId) {
      proDetail = JSON.stringify({
        auctionId: req.query.auctionId
      });
      //通过专场ID查询专场信息
      let result = await http.get(`/api/mallAuction/web/query`, {
        id: req.query.auctionId
      }, '/antique').catch(e => {
        res.send({
          errCode: 401
        });
      });
      auctionDetail = result.data;
    }

    res.render('add_auction_collect', {
      proDetail: proDetail,
      auctionId:req.query.auctionId,
      isNew:true,
      auctionDetail:auctionDetail?auctionDetail:null
    });
  });

  //上传普通藏品
  app.get(`${namespace.root}/collect/add`, async function (req, res) {
    res.render('add_collect', {
      proDetail: ''
    });
  });

  //修改藏品
  app.get(`${namespace.root}/collect/update`, async function (req, res) {
    let result = {};
    let resultData = {};
    let type = req.query.type;
    let isOutStock = req.query.isOutStock;
    if (req.query && req.query.id) {
      result = await http.get(`/api/mallProduct/manage/query`, {
        token: req.cookies.token,
        id: req.query.id
      }, '/antique').catch(e => {
        res.send({
          errCode: 401
        });
      });
    }
    if (result && result.data) {
      resultData = result.data;
      var date = new Date(resultData.planEndDate);
      var Y = date.getFullYear() + '-';
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var D = date.getDate() + ' ';
      var h = date.getHours() + ':';
      var m = date.getMinutes();
      var s = date.getSeconds();
      var time = Y + M + D + h + m;
      resultData.planEndDate = time;
    }
    result = JSON.stringify(resultData);
    if (type == 1 || type == 3) {
      res.render('add_auction_collect', {
        proDetail: result,
        isOutStock: isOutStock?isOutStock:false,
        type:type
      });
    } else if (type == 2) {
      res.render('add_collect', {
        proDetail: result,
        isOutStock: isOutStock?isOutStock:false
      });
    }
  });
};